package com.webservice;

import com.carpark.ejb.DAO.DAO;
import com.carpark.ejb.DAO.DaoImpl;
import com.carpark.ejb.Model.CarsModel;
import com.carpark.ejb.Model.ClientsModel;
import com.carpark.ejb.Model.ParkingpassesModel;
import com.carpark.ejb.Model.ParkingspacesModel;

import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.Application;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

/**
 *
 * @author Krzysztof
 */
@ManagedBean
@ViewScoped
public class CarBean implements Serializable {

    private static final long serialVersionUID = 1L;
  private final DAO dao = getSingleDao();
    DaoSingleObject dso;
    private Integer CAR_ID;
    private String NUMBER_PLATE;
    private String MAKE;
    private String MODEL;
    private String CAR_TYPE;
    private String COMMENTS;
    private String radioOptions;
    private List<CarsModel> carsByClient;
    private LoginBean lg;
    ClientsModel currentUser;
    private CarsModel selectedCar;
    private SelectItem[] carsOptions;
    ParkingpassesModel currentPass;
    Integer currentPassId;
    SelectItem[] carsByClientOptions;

    @PostConstruct
    public void init() {
        Logger.getLogger(CarBean.class.getName()).log(Level.SEVERE, "[CarPark] construct cars");
        setCurrentUser(dao.getClientById(getUserID()));
        carsOptions = createFilterOptionsForCarType();
        carsByClientOptions = createSelectItemsCarByClient();
    }

    public Integer getUserID() {
        if (this.lg == null) {
            FacesContext context = FacesContext.getCurrentInstance();
            Application app = context.getApplication();
            this.lg = (LoginBean) app.evaluateExpressionGet(context, "#{loginBean}", LoginBean.class);
        }
        return this.lg.getId();
    }
  public DAO getSingleDao() {
        if (this.dso == null) {
            FacesContext context = FacesContext.getCurrentInstance();
            Application app = context.getApplication();
            this.dso = (DaoSingleObject) app.evaluateExpressionGet(context, "#{daoSingleObject}", DaoSingleObject.class);
        }
        return this.dso.dao;
    }
    public Integer getCurrentPassId() {
        return currentPassId;
    }

    public void setCurrentPassId(Integer currentPassId) {
        this.currentPassId = currentPassId;
    }

    public LoginBean getLg() {
        return lg;
    }

    public SelectItem[] getCarsByClientOptions() {
        
        return carsByClientOptions;
    }
 public void serverLog(String x) {
        Logger.getLogger(CarBean.class.getName()).log(Level.SEVERE, "[Carpark2013]: " + x);
    }


    public void setCarsByClientOptions(SelectItem[] carsByClientOptions) {
        this.carsByClientOptions = carsByClientOptions;
    }

    public void setLg(LoginBean lg) {
        this.lg = lg;
    }

    public SelectItem[] getCarsOptions() {
        return carsOptions;
    }

    public void setCarsOptions(SelectItem[] carsOptions) {
        this.carsOptions = carsOptions;
    }

    public ClientsModel getCurrentUser() {
        return currentUser;
    }

    public ParkingpassesModel getCurrentPass() {
        return currentPass;
    }

    public void setCurrentPass(ParkingpassesModel currentPass) {
        Logger.getLogger(CarBean.class.getName()).log(Level.SEVERE, "[CarPark] setting pass" + currentPass);
        this.currentPass = currentPass;
        this.currentPassId = currentPass.getParkingPassId();
    }

    public void setCurrentUser(ClientsModel currentUser) {
        this.currentUser = currentUser;
    }

    public List<CarsModel> getCarsByClient() {
        if (carsByClient == null) {
            this.carsByClient = dao.getCarsByClient(currentUser);

        }
        return this.carsByClient;
    }

    public void setCarsByClient(List<CarsModel> carsByClient) {
        this.carsByClient = carsByClient;
    }

    public Integer getCAR_ID() {
        return CAR_ID;
    }

    public void setCAR_ID(Integer CAR_ID) {
        this.CAR_ID = CAR_ID;
    }

    public String getNUMBER_PLATE() {
        return NUMBER_PLATE;
    }

    public void setNUMBER_PLATE(String NUMBER_PLATE) {
        Logger.getLogger(CarBean.class.getName()).log(Level.SEVERE, "[CarPark] setting plate" + NUMBER_PLATE);
        this.NUMBER_PLATE = NUMBER_PLATE;
    }

    public String getMAKE() {
        return MAKE;
    }

    public void setMAKE(String MAKE) {
        this.MAKE = MAKE;
    }

    public String getMODEL() {
        return MODEL;
    }

    public void setMODEL(String MODEL) {
        this.MODEL = MODEL;
    }

    public String getCAR_TYPE() {
        return CAR_TYPE;
    }

    public void setCAR_TYPE(String CAR_TYPE) {
        Logger.getLogger(CarBean.class.getName()).log(Level.SEVERE, "[CarPark] setting cartype" + CAR_TYPE);
        this.CAR_TYPE = CAR_TYPE;
    }

    public String getCOMMENTS() {
        return COMMENTS;
    }

    public void setCOMMENTS(String COMMENTS) {
        this.COMMENTS = COMMENTS;
    }

    public CarBean() {
    }

    public CarsModel getSelectedCar() {
        return selectedCar;
    }

    public String getRadioOptions() {
        return radioOptions;
    }

    public void setRadioOptions(String radioOptions) {
        this.radioOptions = radioOptions;
    }

    public void setSelectedCar(CarsModel selectedCar) {
        this.selectedCar = selectedCar;
    }

    public void addCarToDatabase() throws Exception {
        Logger.getLogger(CarBean.class.getName()).log(Level.SEVERE, "[CarPark] adding Car");
        if (dao.addCar(new CarsModel(NUMBER_PLATE, MAKE, MODEL, CAR_TYPE)) == null) {
            Logger.getLogger(CarBean.class.getName()).log(Level.SEVERE, "[CarPark] Exceptions: null " + NUMBER_PLATE + " " + MAKE + " " + MODEL + " " + CAR_TYPE);
        } else {
            Logger.getLogger(CarBean.class.getName()).log(Level.SEVERE, "[CarPark] Exceptions: success ");
        }
    }

    private SelectItem[] createFilterOptionsForCarType() {
        SelectItem[] options = new SelectItem[5];

        options[0] = new SelectItem("", "Wybierz");

        options[1] = new SelectItem(carTypes.Motorcycle.getType(), carTypes.Motorcycle.getLabel());
        options[2] = new SelectItem(carTypes.Van.getType(), carTypes.Van.getLabel());
        options[3] = new SelectItem(carTypes.Trailer.getType(), carTypes.Trailer.getLabel());
        options[4] = new SelectItem(carTypes.Personal.getType(), carTypes.Personal.getLabel());

        return options;

    }
     public String getLabelFromCarsTypeValue(ParkingspacesModel space) {
        for (int i = 0; i < carsOptions.length; i++) {
            if (carsOptions[i].getValue().equals(space.getType())) {
                return carsOptions[i].getLabel();
            }
        }
        return "brak";
    }

   


    private SelectItem[] createSelectItemsCarByClient() {
        List<CarsModel> cars = getCarsByClient();
        SelectItem[] options = new SelectItem[cars.size() + 1];

        options[0] = new SelectItem("", "Wybierz");
        int i = 1;
        for (CarsModel cr : cars) {
            options[i] = new SelectItem(cr.getCarId(),  cr.getMake()+" "+cr.getModel()+" "+cr.getNumberPlate());
            i++;
        }

        return options;

    }

    public void updatePassWithSelectedCar() throws Exception {
        Logger.getLogger(CarBean.class.getName()).log(Level.SEVERE, "[CarPark] updating pass");
        if (dao.updatePass(currentPass, selectedCar) == null) {
            Logger.getLogger(CarBean.class.getName()).log(Level.SEVERE, "[CarPark] Exceptions: null " + currentPass.getParkingPassId() + " " + selectedCar);
        } else {
            Logger.getLogger(CarBean.class.getName()).log(Level.SEVERE, "[CarPark] updating success ");
        }
    }

    public void updatePassWithNewCar() throws Exception {

        CarsModel cr = new CarsModel(NUMBER_PLATE, MAKE, MODEL, CAR_TYPE);

        Logger.getLogger(CarBean.class.getName()).log(Level.SEVERE, "[CarPark] updating pass" + currentPass + " " + NUMBER_PLATE + " " + MAKE + " " + MODEL + " " + CAR_TYPE);
        if (dao.updatePass(currentPass, cr) == null) {
            Logger.getLogger(CarBean.class.getName()).log(Level.SEVERE, "[CarPark] Exceptions: null " + currentPass + " " + cr.getCarType() + " " + cr.getMake() + " " + cr.getModel());
        } else {
            Logger.getLogger(CarBean.class.getName()).log(Level.SEVERE, "[CarPark] updating success ");
        }
    }

    public void updatePass() throws Exception {
        Logger.getLogger(CarBean.class.getName()).log(Level.SEVERE, "[CarPark] updating pass");
        if (radioOptions.equals("1")) {
            updatePassWithSelectedCar();
        }
        if (radioOptions.equals("2")) {
            updatePassWithNewCar();
        }
    }
}
